<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏平台 - 注册</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>tailwind.config = {theme: {extend: {colors: {primary: '#3B82F6'}}}}</script>
    <style type="text/tailwindcss">@layer utilities { .content-auto { content-visibility: auto; } }</style>
</head>
<body class="bg-gradient-to-br from-blue-50 to-indigo-50 min-h-screen font-sans">
<!-- 导航栏 -->
<nav class="bg-white shadow-md sticky top-0 z-50">
    <div class="container mx-auto px-4 py-3 flex justify-between items-center">
        <a href="/webapp/static" class="text-2xl font-bold text-primary flex items-center"><i class="fa fa-gamepad mr-2"></i>GamePlatform</a>
        <div class="hidden md:flex space-x-6 items-center">
            <a href="/game.html" class="text-gray-700 hover:text-primary transition-colors">游戏中心</a>
            <a href="/scoreboard.html" class="text-gray-700 hover:text-primary transition-colors">排行榜</a>
            <a href="/login.html" class="px-4 py-2 bg-primary text-white rounded-md hover:shadow-lg hover:-translate-y-0.5 transition-all">登录</a>
        </div>
        <button class="md:hidden text-gray-700" id="mobileMenuBtn"><i class="fa fa-bars text-xl"></i></button>
    </div>
    <div id="mobileMenu" class="hidden md:hidden bg-white border-t"><div class="container mx-auto px-4 py-3 flex flex-col space-y-3">
        <a href="/game.html" class="text-gray-700 hover:text-primary py-2">游戏中心</a>
        <a href="/scoreboard.html" class="text-gray-700 hover:text-primary py-2">排行榜</a>
        <a href="/login.html" class="px-4 py-2 bg-primary text-white rounded-md hover:shadow-lg transition-all text-center">登录</a>
    </div></div>
</nav>

<!-- 主内容 -->
<main class="container mx-auto px-4 py-12">
    <div class="max-w-2xl mx-auto">
        <div class="text-center mb-12">
            <h1 class="text-[clamp(1.8rem,4vw,2.5rem)] font-bold text-gray-800 mb-4">创建新账户</h1>
            <p class="text-lg text-gray-600">加入我们，开始你的游戏之旅</p>
        </div>

        <div class="bg-white rounded-xl shadow-md p-8 max-w-md mx-auto">
            <form id="registerForm" class="space-y-6">
                <div>
                    <label for="username" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-user text-gray-400"></i>
                        </div>
                        <input type="text" id="username" name="username" class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请设置用户名" required>
                        <div id="usernameFeedback" class="absolute inset-y-0 right-0 pr-3 flex items-center text-sm text-gray-500 hidden"></div>
                    </div>
                </div>

                <div>
                    <label for="email" class="block text-sm font-medium text-gray-700 mb-1">邮箱</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-envelope text-gray-400"></i>
                        </div>
                        <input type="email" id="email" name="email" class="block w-full pl-10 pr-3 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请输入邮箱" required>
                    </div>
                </div>

                <div>
                    <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-gray-400"></i>
                        </div>
                        <input type="password" id="password" name="password" class="block w-full pl-10 pr-10 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请设置密码" required>
                        <button type="button" id="showPassword" class="absolute inset-y-0 right-0 pr-3 flex items-center text-gray-400 hover:text-gray-600">
                            <i class="fa fa-eye-slash"></i>
                        </button>
                    </div>
                </div>

                <div>
                    <label for="confirmPassword" class="block text-sm font-medium text-gray-700 mb-1">确认密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-gray-400"></i>
                        </div>
                        <input type="password" id="confirmPassword" name="confirmPassword" class="block w-full pl-10 pr-10 py-3 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors" placeholder="请确认密码" required>
                        <div id="passwordFeedback" class="absolute inset-y-0 right-0 pr-3 flex items-center text-sm text-gray-500 hidden"></div>
                    </div>
                </div>

                <div>
                    <button type="submit" class="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary transition-all duration-300 hover:shadow-lg hover:-translate-y-0.5">
                        注册
                    </button>
                </div>
            </form>

            <div class="mt-6 text-center">
                <p class="text-sm text-gray-600">
                    已有账户？ <a href="/login.html" class="font-medium text-primary hover:underline">立即登录</a>
                </p>
            </div>
        </div>
    </div>
</main>

<!-- 页脚 -->
<footer class="bg-gray-800 text-white py-8">
    <div class="container mx-auto px-4 text-center">
        <p class="text-gray-400">© 2025 GamePlatform. 保留所有权利。</p>
    </div>
</footer>

<!-- 通知组件 -->
<div id="toast" class="fixed top-20 right-4 max-w-xs bg-white rounded-lg shadow-lg p-4 transform transition-all duration-300 translate-x-full z-50"><div class="flex items-start">
    <div class="mr-3 mt-1"><i class="fa fa-info-circle text-primary text-lg"></i></div>
    <div class="flex-1"><h4 id="toastTitle" class="font-bold text-gray-800">通知</h4>
        <p id="toastMsg" class="text-gray-600 text-sm mt-1">内容将显示在这里</p>
    </div>
    <button id="closeToast" class="text-gray-400 hover:text-gray-600"><i class="fa fa-times"></i></button>
</div></div>

<script>
    // 显示通知
    function showToast(title, msg, type = 'info') {
        const toast = document.getElementById('toast');
        const titleEl = document.getElementById('toastTitle');
        const msgEl = document.getElementById('toastMsg');
        const icon = toast.querySelector('i');

        titleEl.textContent = title;
        msgEl.textContent = msg;

        // 设置图标和颜色
        icon.className = 'mr-3 mt-1 fa';
        if (type === 'success') {
            icon.classList.add('fa-check-circle', 'text-green-500');
        } else if (type === 'error') {
            icon.classList.add('fa-exclamation-circle', 'text-red-500');
        } else {
            icon.classList.add('fa-info-circle', 'text-primary');
        }

        // 显示通知
        toast.classList.remove('translate-x-full');
        toast.classList.add('translate-x-0');

        // 自动关闭
        setTimeout(() => {
            toast.classList.remove('translate-x-0');
            toast.classList.add('translate-x-full');
        }, 3000);
    }

    // 移动端菜单切换
    document.getElementById('mobileMenuBtn').addEventListener('click', () => {
        document.getElementById('mobileMenu').classList.toggle('hidden');
    });

    // 显示/隐藏密码
    document.getElementById('showPassword').addEventListener('click', () => {
        const passwordInput = document.getElementById('password');
        const confirmInput = document.getElementById('confirmPassword');
        const icon = document.getElementById('showPassword').querySelector('i');

        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';
            confirmInput.type = 'text';
            icon.classList.remove('fa-eye-slash');
            icon.classList.add('fa-eye');
        } else {
            passwordInput.type = 'password';
            confirmInput.type = 'password';
            icon.classList.remove('fa-eye');
            icon.classList.add('fa-eye-slash');
        }
    });

    // 用户名验证
    document.getElementById('username').addEventListener('blur', () => {
        const username = document.getElementById('username').value.trim();
        const feedback = document.getElementById('usernameFeedback');

        if (!username) {
            feedback.textContent = '请输入用户名';
            feedback.classList.remove('hidden', 'text-green-500', 'fa-check-circle');
            feedback.classList.add('text-red-500', 'fa-exclamation-circle');
            return;
        }

        if (username.length < 3) {
            feedback.textContent = '用户名至少3个字符';
            feedback.classList.remove('hidden', 'text-green-500', 'fa-check-circle');
            feedback.classList.add('text-red-500', 'fa-exclamation-circle');
            return;
        }

        // 模拟API验证
        feedback.innerHTML = '<i class="fa fa-spinner fa-spin mr-1"></i> 验证中...';
        feedback.classList.remove('hidden', 'text-red-500', 'fa-exclamation-circle');

        setTimeout(() => {
            if (username === 'admin' || username === 'test') {
                feedback.textContent = '用户名已存在';
                feedback.classList.remove('text-green-500', 'fa-check-circle');
                feedback.classList.add('text-red-500', 'fa-exclamation-circle');
            } else {
                feedback.textContent = '用户名可用';
                feedback.classList.remove('text-red-500', 'fa-exclamation-circle');
                feedback.classList.add('text-green-500', 'fa-check-circle');
            }
        }, 800);
    });

    // 密码一致性验证
    document.getElementById('confirmPassword').addEventListener('blur', () => {
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;
        const feedback = document.getElementById('passwordFeedback');

        if (password !== confirmPassword) {
            feedback.textContent = '两次密码不一致';
            feedback.classList.remove('hidden', 'text-green-500', 'fa-check-circle');
            feedback.classList.add('text-red-500', 'fa-exclamation-circle');
        } else if (password && confirmPassword) {
            feedback.textContent = '密码一致';
            feedback.classList.remove('hidden', 'text-red-500', 'fa-exclamation-circle');
            feedback.classList.add('text-green-500', 'fa-check-circle');
        }
    });

    // 注册表单提交
    document.getElementById('registerForm').addEventListener('submit', (e) => {
        e.preventDefault();
        const username = document.getElementById('username').value.trim();
        const email = document.getElementById('email').value.trim();
        const password = document.getElementById('password').value.trim();
        const confirmPassword = document.getElementById('confirmPassword').value.trim();

        // 基本验证
        let isValid = true;
        const usernameFeedback = document.getElementById('usernameFeedback');
        const passwordFeedback = document.getElementById('passwordFeedback');

        if (!username) {
            usernameFeedback.textContent = '请输入用户名';
            usernameFeedback.classList.remove('hidden', 'text-green-500', 'fa-check-circle');
            usernameFeedback.classList.add('text-red-500', 'fa-exclamation-circle');
            isValid = false;
        }

        if (!email) {
            showToast('输入错误', '请输入邮箱', 'error');
            isValid = false;
        }

        if (!password || password.length < 6) {
            showToast('输入错误', '密码至少6个字符', 'error');
            isValid = false;
        }

        if (password !== confirmPassword) {
            passwordFeedback.textContent = '两次密码不一致';
            passwordFeedback.classList.remove('hidden', 'text-green-500', 'fa-check-circle');
            passwordFeedback.classList.add('text-red-500', 'fa-exclamation-circle');
            isValid = false;
        }

        if (!isValid) return;

        // 显示加载状态
        const submitBtn = document.querySelector('button[type="submit"]');
        const originalText = submitBtn.innerHTML;
        submitBtn.disabled = true;
        submitBtn.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i> 注册中...';

        // 模拟API请求
        fetch('/api/auth/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ username, email, password })
        })
            .then(res => {
                submitBtn.disabled = false;
                submitBtn.innerHTML = originalText;

                if (res.ok) {
                    return res.json();
                } else if (res.status === 400) {
                    throw new Error('用户名已存在');
                } else {
                    throw new Error('服务器错误，请稍后再试');
                }
            })
            .then(data => {
                showToast('注册成功', `账户创建成功，欢迎 ${data.username}`, 'success');
                setTimeout(() => {
                    window.location.href = '/login.html';
                }, 1500);
            })
            .catch(err => {
                showToast('注册失败', err.message, 'error');
            });
    });

    // 页面加载完成后执行
    document.addEventListener('DOMContentLoaded', () => {
        // 自动聚焦到用户名输入框
        document.getElementById('username').focus();
    });
</script>
</body>
</html>